//
//  MHCategoryDetailViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHCategoryDetailViewController.h"
#import "MHComicIntroductionCell.h"
#import "MHComicIntroduct.h"
#import <AFNetworking.h>

@interface MHCategoryDetailViewController ()
@property (nonatomic, strong) NSMutableArray *comicIntroductListArray;
@property (nonatomic, copy) NSString *acquireSite;
@end

@implementation MHCategoryDetailViewController

static NSString * const reuseIdentifier = @"ComicIntroduction";

- (void)acquireData
{
    [[AFHTTPRequestOperationManager manager] GET:self.acquireSite parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        self.comicIntroductListArray = [NSMutableArray array];
        for (NSDictionary *dict in responseObject[@"Booklist"]) {
            MHComicIntroduct *comicIntroduct = [MHComicIntroduct modelWithDict:dict];
            [self.comicIntroductListArray addObject:comicIntroduct];
        }
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
}

- (void)setCatalogID:(NSInteger)CatalogID
{
    _CatalogID = CatalogID;
    self.acquireSite = [NSString stringWithFormat:@"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx?cataID=%ld&method=booklist&pageIndex=0&pageSize=24&state=0&order=0", (long)CatalogID];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    CGFloat margin = 20;
    CGFloat padding = 10;
    NSInteger row = 3;
    CGFloat itemW = (Width - 2 * padding - (row - 1) * padding) / row;
    CGFloat itemH = (itemW - padding) * 11.0 / 9.0 + 35;
    flowLayout.itemSize = CGSizeMake(itemW, itemH);
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, padding, margin + 44, padding);
    flowLayout.minimumInteritemSpacing = 0;
    
    
    [self acquireData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.comicIntroductListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MHComicIntroductionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.comicIntroduct = self.comicIntroductListArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.pushComicDetailVCBlock != nil) {
        self.pushComicDetailVCBlock(self.comicIntroductListArray[indexPath.row]);
    }
}

@end
